package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.coupon.dao.mapper.CouponCodeMapper;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.pojo.po.CouponCodePO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponCodeRepositoryImpl implements CouponCodeRepository {
    private static final Logger log = LoggerFactory.getLogger(CouponCodeRepositoryImpl.class);
    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Override
    public String getCouponCode(Integer couponId) {
        log.info("入参couponId: {}", couponId);
        CouponCodePO couponCodePO = new CouponCodePO();
        QueryWrapper queryWrapper = new QueryWrapper(couponCodePO);
        queryWrapper.eq("coupon_id", couponId);
        queryWrapper.eq("status", 1);
        List<CouponCodePO> list = couponCodeMapper.selectList(queryWrapper);
        couponCodePO = list.get(0);
        log.info("couponCodePO: {}", couponCodePO);
        String couponCode = couponCodePO.getCouponCode();
        log.info("get coupon code: {}", couponCode);
        return couponCode;
    }

    @Override
    public void insert(CouponCodePO couponCodePO) {
       couponCodeMapper.insert(couponCodePO);
    }

    @Override
    public CouponCodePO getByCouponCode(String couponCode) {
        CouponCodePO couponCodePO = new CouponCodePO();
        QueryWrapper queryWrapper = new QueryWrapper(couponCodePO);
        queryWrapper.eq("coupon_code", couponCode);
        couponCodePO = couponCodeMapper.selectOne(queryWrapper);
        return couponCodePO;
    }

    @Override
    public void update(CouponCodePO couponCodePO) {
        log.info("update couponCodePO: {}", couponCodePO);
        couponCodeMapper.updateById(couponCodePO);
    }
}
